package com.hyh.user.util;


import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.hyh.user.domain.User;

import java.util.Calendar;


public class JwtUtil {
    // 生成token
    // Algorithm.HMAC256():使用HS256生成token,密钥则是用户的密码
    private static final String SING = "ShoppingProject";

    public static String getToken(User user) {
        //System.out.println(String.valueOf(user.getId()));
        // 设置token过期时间
        Calendar instance= Calendar.getInstance();
        instance.add(Calendar.DATE,10);
        String token="";
        token= JWT.create()
                .withClaim("id",String.valueOf(user.getId())) //设置载荷
                .withClaim("number",user.getNumber())
                .withExpiresAt(instance.getTime()) //设置令牌过期的时间
                .sign(Algorithm.HMAC256(SING));

        return token;
    }
    /**，
     * 验证token  合法性
     */
    public static DecodedJWT verify(String token) {
        return JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
    }
}
